# -*- coding: utf-8 -*-
# @ 来源平台：
# @ 来源网址：
# @ 时间：  2022/3/8 10:34
# Tool ：PyCharm
# @ author: LZL
# 目录：
# 数据：   
# @ 网站

import requests
from conf.pingtai_setting import pingtai_conf
headers = {
    'Authorization': 'Bearer kasjdflqjkeofawejifjfglewjemrgopwekrgopwerjkgopqwerjksgogkp'
}
domain_name = "http://midd.com"


def show_list():
    url = domain_name + "/api/tender/get_source_list"
    res = requests.get(url, headers=headers)
    return res.json().get("data").get("list")


def show_id(staute_id):
    url = domain_name + f"/api/tender/get_source_info?site_code={staute_id}"
    res = requests.get(url, headers=headers)
    print(res.json())
    return res.json()

def show_zb_id(status_id,name):
    url = domain_name + f"/api/tender/get_source_info?site_code={status_id}"
    res = requests.get(url, headers=headers)
    for i in res.json().get('data').get("tagging_list"):
        if i.get("tagging_name") == name:
            return i.get("tagging_code")
    else:
        return

def add_PT(name):
    url = domain_name + "/api/tender/add_source"
    datas = {
        "site_name": name
    }
    res = requests.post(url, headers=headers, data=datas)
    result = res.json()
    if result["msg"] == "success":
        print(f"添加成功 {name}")
        return result.get("data").get("tagging_code")
    else:
        print(f"添加失败 {name}")
    return


def del_PT(staute_id):
    url = domain_name + "/api/tender/del_source"
    datas = {
        "site_code": staute_id
    }
    res = requests.post(url, headers=headers, data=datas)
    result = res.json()
    if result["code"] == 20000:
        print(f"删除成功 {staute_id}")
    else:
        print(result['msg'])
    return


def up_PT_name(staute_id, name):
    url = domain_name + "/api/tender/update_source_name"
    datas = {
        "site_code": staute_id,
        "new_site_name": name
    }
    res = requests.post(url, headers=headers, data=datas)
    result = res.json()
    if result["code"] == 20000:
        print(f"更新成功 {staute_id}")
    else:
        print(result['msg'])
    return


def up_PT_url(staute_id, sit_url: str):
    url = domain_name + "/api/tender/update_source_url"
    datas = {
        "site_code": staute_id,
        "site_url[]": sit_url
    }
    res = requests.post(url, headers=headers, data=datas)
    result = res.json()
    if result["code"] == 20000:
        print(f"更新成功 {staute_id}")
    else:
        print(result['msg'])
    return


# 流程标注
def add_ifp(staute_id, tagging_pid, tagging_name):
    url = domain_name + "/api/tender/add_source_tagging"
    datas = {
        "site_code": staute_id,
        "tagging_pid": tagging_pid,
        "tagging_name": tagging_name
    }
    res = requests.post(url, headers=headers, data=datas)
    result = res.json()
    if result["code"] == 20000:
        print(f"add 成功 {staute_id}")
        return result.get("data").get("tagging_code")
    else:
        print(result['msg'])
        return


def update_ifp(staute_id:int, tagging_pid:int,new_tagging_name:str):
    """
    更新平台标注名称 
    staute_id
    tagging_pid
    new_tagging_name
    """
    url = domain_name + "/api/tender/update_source_tagging_name"
    datas = {
        "site_code": staute_id,
        "tagging_pid": tagging_pid,
        "tagging_name": new_tagging_name
    }
    res = requests.post(url, headers=headers, data=datas)
    result = res.json()
    if result["code"] == 20000:
        print(f"更新 成功 {staute_id}")
    else:
        print(result['msg'])
    return


def del_ifp(staute_id: int, tagging_pid: int):
    """
    删除平台标注
    site_code 网站编码
    tagging_pid 标注编码
    """
    url = domain_name + "/api/tender/del_source_tagging"
    datas = {
        "site_code": staute_id,
        "tagging_pid": tagging_pid
    }
    res = requests.post(url, headers=headers, data=datas)
    result = res.json()
    if result["code"] == 20000:
        print(f"更新 成功 {staute_id}")
    else:
        print(result['msg'])
    return


def run():
    """
    执行流程
    """
    for tem in pingtai_conf:
        add_PT(tem.get("stie_name"))
    temp_list = show_list()
    print(temp_list)
    for temp in temp_list:
        for pingtai in pingtai_conf:
            if pingtai.get("stie_name") == temp.get("site_name"):
                site_code = temp.get("site_code")
                print(pingtai.get("stie_name"))
                up_PT_url(site_code, pingtai.get("site_url"))
                zhaobiao_tagging_code = add_ifp(site_code,0,"招标信息")
                zhongbiao_tagging_code = add_ifp(site_code,0,"中标信息")
                if not zhaobiao_tagging_code:
                    zhaobiao_tagging_code = show_zb_id(site_code,"招标信息")
                if not zhongbiao_tagging_code:
                    zhongbiao_tagging_code = show_zb_id(site_code,"中标信息")
                print(zhaobiao_tagging_code)
                print(zhongbiao_tagging_code)
                for i in pingtai.get("ifbprogresstag").get("中标信息"):
                    add_ifp(site_code,zhongbiao_tagging_code,i)
                for j in pingtai.get("ifbprogresstag").get("招标信息"):
                    add_ifp(site_code,zhaobiao_tagging_code,j)


if __name__ == '__main__':
    run()